<template>
    <div style="width: 80%">
        <div style="margin-bottom: 30px">新增用户</div>
        <el-form :inline="true" :model="form" :rules="rules" ref="ruleForm" label-width="100px">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
            </el-form-item>
            <!--            <el-form-item label="用户名">-->
            <!--                <el-input v-model="form.username" placeholder="请输入用户名"></el-input>-->
            <!--            </el-form-item>-->
            <!--            由于后端用日期+uuid设置了用户名，所以前端不需要设置-->
            <el-form-item label="年龄" prop="age">
                <el-input v-model="form.age" placeholder="请输入年龄"></el-input>
            </el-form-item>
            <el-form-item label="性别">
                <el-radio v-model="form.sex" label="男">男</el-radio>
                <el-radio v-model="form.sex" label="女">女</el-radio>
            </el-form-item>
            <el-form-item label="联系方式" prop="phone">
                <el-input v-model="form.phone" placeholder="请输入联系方式"></el-input>
            </el-form-item>
            <el-form-item label="地址" prop="address">
                <el-input v-model="form.address" placeholder="请输入地址"></el-input>
            </el-form-item>
        </el-form>

        <div style="text-align: center;margin-top: 30px ">
            <el-button type="primary" @click="addUser" size="medium">提交</el-button>
            <!--            <el-button type="danger">取消</el-button>-->
        </div>
    </div>


</template>

<script>
    import request from "@/utils/request";

    export default {
        name: "AddUser",
        data() {
//校验年龄
            const checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('年龄不能为空'));
                }
                if (!/^[0-9]+$/.test(value)) {
                    callback(new Error('请输入数字值'));
                }
                if (parseInt(value) > 120 || parseInt(value) <= 0){
                    callback(new Error('请输入合理的年龄'));
                }
                    callback()
            };
//校验手机号
            const checkPhone = (rule, value, callback) => {
                if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(value)){
                    callback(new Error('请输入合法的手机号'));
                }
                callback()
            };

            return {
                //默认男
                form: {sex: '男'},
                rules: {
                    //姓名不能为空
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'},
                    ],
                    //年龄
                    age: [
                        {validator: checkAge, trigger: 'blur'}
                    ],
                    //手机号
                    phone:[
                        {validator: checkPhone, trigger: 'blur'}

                    ]
                }
            }
        },
        methods: {
            addUser() {
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        request.post('/user/addUser', this.form).then(res => {
                            if (res.code === '200') {
                                this.$notify.success('新增成功')
                                //默认男 清空表单
                                // this.$ref['ruleForm'].resetFields()
                                this.$router.push("/userList")
                            } else {
                                this.$notify.error(res.msg)
                            }
                        })
                    }
                })


            }

        }
    }
</script>

<!--<style scoped>-->

<!--</style>-->